import pyautogui
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from head.Init_Driver import SurveyExaminationSystem_Driver


class MyProject:
    url = ""
    driver = ""
    # file_path = ""
    def __init__(self):
        # 集成测试
        self.url = "http://8.155.1.153:8081/project"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)

        # 单元测试
        # self.url = "http://8.155.1.153:8081/user/login"
        # self.driver = SurveyExaminationSystem_Driver.driver
        # self.driver.get(self.url)
        # # 初始化登录成功状态
        # self.driver.find_element(By.ID, "username").send_keys("karen")
        # self.driver.find_element(By.ID, "password").send_keys("karen1234")
        # self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        # 检查是否登录成功
        # assert WebDriverWait(self.driver, 5).until(
        #     expected_conditions.visibility_of_element_located((By.XPATH,"//a[h1='问卷系统']")))
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > header > div > div.ant-pro-global-header-right-content > div > div > div")))
        # 点击“我的项目”进入相关页面
        # self.driver.find_element(By.LINK_TEXT,"我的项目").click()
        # time.sleep(3)
        self.driver.maximize_window()

    def MyProjectUIFuncTest(self):
        # 测试“新建”按钮
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='新建']")))
        self.driver.find_element(By.XPATH,"//span[text()='新建']").click()
        # 点击“问卷调查”
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='问卷调查']")))
        self.driver.find_element(By.XPATH,"//span[text()='问卷调查']").click()
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(2)")))
        # 回退至“我的项目”页面
        self.driver.back()

        # 测试“新建”按钮
        WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        # 点击“在线考试”
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='在线考试']")))
        self.driver.find_element(By.XPATH,"//span[text()='在线考试']").click()
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(2)")))
        # 回退至“我的项目”页面
        self.driver.back()

        # 测试“新建”按钮
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        # 点击“通过Excel导入(快查)”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='通过Excel导入(快查)']")))
        self.driver.find_element(By.XPATH,"//span[text()='通过Excel导入(快查)']").click()
        # 点击文件上传对话框空白处弹出上传对话框
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.XPATH,"//p[text()='点击或者拖拽Excel文件到此处']"))).click()
        # 上传文件
        # time.sleep(1)
        pyautogui.sleep(1)
        pyautogui.write("D:\\Desktop\\TestFile.xlsx")
        time.sleep(1)
        pyautogui.press('enter')
        # pyautogui.sleep(1)
        # 获取屏幕分辨率
        print(f'当前屏幕分辨率为：{pyautogui.size()}')
        # 获取鼠标要点击的地方
        print(f'鼠标需要移动到：{pyautogui.position()}')
        # 移动要点击的地方
        pyautogui.moveTo(940,850,duration=1)
        # 单击左键
        pyautogui.click(button='left')
        # 点击导入上传
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='导 入']")))
        self.driver.find_element(By.XPATH, "//span[text()='导 入']").click()
        # 点击跳转
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='跳 转']")))
        self.driver.find_element(By.XPATH, "//span[text()='跳 转']").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(4)")))
        time.sleep(2)
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 回退至“我的项目”页面
        self.driver.back()
        # 测试上传文件对话框“取消”按钮是否正常
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='通过Excel导入(快查)']")))
        self.driver.find_element(By.XPATH, "//span[text()='通过Excel导入(快查)']").click()
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//p[text()='点击或者拖拽Excel文件到此处']")))
        self.driver.find_element(By.XPATH, "//span[text()='取 消']").click()
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))

        # 测试“新建”按钮
        WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        # 点击“通过文本导入”
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='通过文本导入']")))
        self.driver.find_element(By.XPATH,"//span[text()='通过文本导入']").click()
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='文本导入问卷']")))
        # SurveyExaminationSystem_Driver.Get_ScreenShot()
        self.driver.find_element(By.XPATH,"//span[text()='创建项目']").click()
        # 检查是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(2)")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 回退至“我的项目”页面
        self.driver.back()
        # 跳转至相关页面时“关闭”按钮是否正常
        WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='通过文本导入']")))
        self.driver.find_element(By.XPATH,"//span[text()='通过文本导入']").click()
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='文本导入问卷']")))
        self.driver.find_element(By.XPATH,"//span[text()='关 闭']").click()
        self.driver.refresh()

        # 测试“新建”按钮
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        # 点击“通过模板导入”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='通过模板导入']")))
        self.driver.find_element(By.XPATH,"//span[text()='通过模板导入']").click()
        # 点击文件上传对话框空白处弹出上传对话框
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.XPATH,"//p[text()='点击或者拖拽模板文件到此处']"))).click()
        # 上传json文件
        time.sleep(1)
        pyautogui.write("D:\Desktop\TestFile.json")
        time.sleep(1)
        pyautogui.press('enter')
        # pyautogui.sleep(1)
        # 获取屏幕分辨率
        print(f'当前屏幕分辨率为：{pyautogui.size()}')
        # 获取鼠标要点击的地方
        print(f'鼠标需要移动到：{pyautogui.position()}')
        # 移动要点击的地方，在一秒内
        pyautogui.moveTo(940,850,duration=1)
        # 单击左键
        pyautogui.click(button='left')
        time.sleep(1)
        # 点击导入上传
        self.driver.find_element(By.XPATH, "//span[text()='导 入']").click()
        # 检查是否跳转至相关页面
        # assert WebDriverWait(self.driver, 3).until(
        #    expected_conditions.visibility_of_element_located(
        #       (By.CSS_SELECTOR,"#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(4)")))
        time.sleep(2)
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 回退至“我的项目”页面
        self.driver.back()

        # 测试“新建”按钮
        self.driver.refresh()
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located((By.XPATH, "//span[text()='新建']")))
        self.driver.find_element(By.XPATH, "//span[text()='新建']").click()
        # 点击“新建文件夹”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='新建文件夹']")))
        self.driver.find_element(By.XPATH,"//span[text()='新建文件夹']").click()
        # 检查是否弹出“创建文件夹”对话框
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.ID,"name")));
        # 在输入框中输入要创建的文件名称
        self.driver.find_element(By.ID,"name").send_keys("测试用文件")
        # 点击确定
        self.driver.find_element(By.XPATH, "//span[text()='确 定']").click()
        # 查看文件是否创建成功
        assert WebDriverWait(self.driver, 3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR, "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()
        # 测试“我的项目”详情页列表显示方式功能
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "span[class='ant-radio-button']")))
        self.driver.find_element(By.CSS_SELECTOR,"label[class='ant-radio-button-wrapper']").click()
        WebDriverWait(self.driver,5).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"th[class='ant-table-cell']")))
        time.sleep(1)
        SurveyExaminationSystem_Driver.Get_ScreenShot()


# myProject = MyProject()
# myProject.MyProjectUIFuncTest()
# myProject.driver.quit()